package com.cw.jeeyt.dao.impl.system;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.cw.jeeyt.dao.intf.system.SystemProjectInfoDao;
import com.cw.jeeyt.dao.mapper.system.SystemProjectInfoMapper;
import com.cw.jeeyt.dao.model.system.SystemProjectInfoDo;
import com.cw.jeeyt.dao.model.system.SystemProjectInfoExample;
import com.cw.jeeyt.dao.model.system.SystemProjectInfoKeyDo;
import com.cw.jeeyt.dao.query.system.SystemProjectInfoQuery;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.common.utils.PageUtil;
import com.cw.lang.common.utils.StringUtil;
import com.cw.lang.common.dao.AbstractBaseDaoImpl;
import com.cw.lang.common.pagehelper.PageHelper;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * 项目信息 dao数据库操作接口
 *
 * @author chenw
 * @version 1.0.0
 */
@Repository
public class SystemProjectInfoDaoImpl extends AbstractBaseDaoImpl<SystemProjectInfoDo, SystemProjectInfoMapper>
    implements SystemProjectInfoDao {

    /**
     * 查询所有项目信息
     *
     * @return 项目信息集合
     */
    @Override
    public List<SystemProjectInfoDo> querySystemProjectInfoList() {
        return getMapper().querySystemProjectInfoList();
    }

    /**
     * 删除项目信息
     *
     * @param systemProjectInfo
     */
    @Override
    public void deleteSystemProjectInfo(SystemProjectInfoDo systemProjectInfo) {
        getMapper().deleteSystemProjectInfo(systemProjectInfo);
    }

    /**
     * 修改项目信息
     *
     * @param systemProjectInfo
     */
    @Override
    public void updateSystemProjectInfo(SystemProjectInfoDo systemProjectInfo) {
        getMapper().updateSystemProjectInfo(systemProjectInfo);
    }

    /**
     * 根据主键查询项目信息
     *
     * @param id 主键id
     * @return SystemProjectInfo
     */
    @Override
    public SystemProjectInfoDo getSystemProjectInfoById(String id) {
        return getMapper().getSystemProjectInfoById(id);
    }

    /**
     * 新增项目信息
     *
     * @param systemProjectInfo
     * @return 整数
     */
    @Override
    public Integer insertSystemProjectInfo(SystemProjectInfoDo systemProjectInfo) {
        return getMapper().insertSystemProjectInfo(systemProjectInfo);
    }

    /**
     * 分页查询项目信息
     *
     * @param systemProjectInfo 查询条件
     * @param startNum 开始条数
     * @param pageSize 每页长度
     * @return SystemProjectInfo集合
     */
    @Override
    public PageInfo<SystemProjectInfoDo> queryByPage(SystemProjectInfoDo systemProjectInfo, Integer startNum,
        Integer pageSize) {
        PageHelper.startPage(PageUtil.calPageNum(startNum, pageSize), pageSize);
        return new PageInfo<>(getMapper().queryByPage(systemProjectInfo));
    }

    /**
     * 分页查询
     *
     * @param systemProjectInfo 条件
     * @param pageParam 分页参数
     * @return
     */
    @Override
    public PageInfo<SystemProjectInfoDo> selectPage(SystemProjectInfoQuery systemProjectInfo, PageParam pageParam) {
        SystemProjectInfoExample example = buildExample(systemProjectInfo);
        return PageHelper.<SystemProjectInfoDo>startPage(pageParam.getPageNum(),
            pageParam.getPageSize()).doSelectPageInfo(() -> getMapper().selectByExample(example));
    }

    /**
     * 根据key查询项目信息
     *
     * @param id
     * @return
     */
    @Override
    public SystemProjectInfoDo selectByKey(String id) {
        SystemProjectInfoKeyDo systemProjectInfoKeyDo = new SystemProjectInfoKeyDo();
        systemProjectInfoKeyDo.setId(id);
        return getMapper().selectByPrimaryKey(systemProjectInfoKeyDo);
    }

    /**
     * 根据key删除项目信息
     *
     * @param id
     * @return
     */
    @Override
    public int deleteByKey(String id) {
        SystemProjectInfoKeyDo systemProjectInfoKeyDo = new SystemProjectInfoKeyDo();
        systemProjectInfoKeyDo.setId(id);
        return getMapper().deleteByPrimaryKey(systemProjectInfoKeyDo);
    }

    /**
     * 查询项目信息信息
     *
     * @param systemProjectInfo 条件
     * @return List<SystemProjectInfoDo>
     */
    @Override
    public List<SystemProjectInfoDo> selectByExample(SystemProjectInfoQuery systemProjectInfo) {
        return getMapper().selectByExample(buildExample(systemProjectInfo));
    }

    /**
     * 新增项目信息信息
     *
     * @param systemProjectInfo 条件
     * @return int>
     */
    @Override
    public int insertBySelective(SystemProjectInfoDo systemProjectInfo) {
        if (systemProjectInfo == null) {
            return -1;
        }
        systemProjectInfo.setCreateDate(new Date());
        systemProjectInfo.setUpdateDate(new Date());
        return getMapper().insertSelective(systemProjectInfo);
    }

    /**
     * 修改项目信息信息
     *
     * @param systemProjectInfo
     * @return
     */
    @Override
    public int updateBySelective(SystemProjectInfoDo systemProjectInfo) {
        if (systemProjectInfo == null) {
            return -1;
        }
        systemProjectInfo.setUpdateDate(new Date());
        return getMapper().updateByPrimaryKeySelective(systemProjectInfo);
    }

    /**
     * 构建项目信息Example信息
     *
     * @param systemProjectInfo
     * @return
     */
    public SystemProjectInfoExample buildExample(SystemProjectInfoQuery systemProjectInfo) {
        SystemProjectInfoExample example = new SystemProjectInfoExample();
        SystemProjectInfoExample.Criteria criteria = example.createCriteria();
        if (systemProjectInfo != null) {
            //添加查询条件
            if (StringUtil.isNotBlank(systemProjectInfo.getId())) {
                criteria.andIdEqualTo(systemProjectInfo.getId());
            }
            if (CollectionUtil.isNotEmpty(systemProjectInfo.getIds())) {
                criteria.andIdIn(systemProjectInfo.getIds());
            }
            if (StringUtil.isNotEmpty(systemProjectInfo.getProjectCode())) {
                criteria.andProjectCodeEqualTo(systemProjectInfo.getProjectCode());
            }
            if (StringUtil.isNotEmpty(systemProjectInfo.getProjectName())) {
                criteria.andProjectNameEqualTo(systemProjectInfo.getProjectName());
            }
            if (StringUtil.isNotEmpty(systemProjectInfo.getProjectType())) {
                criteria.andProjectTypeEqualTo(systemProjectInfo.getProjectType());
            }
            if (StringUtil.isNotEmpty(systemProjectInfo.getProjectSvnUrl())) {
                criteria.andProjectSvnUrlEqualTo(systemProjectInfo.getProjectSvnUrl());
            }
            if (StringUtil.isNotEmpty(systemProjectInfo.getProjectWebserviceUrl())) {
                criteria.andProjectWebserviceUrlEqualTo(systemProjectInfo.getProjectWebserviceUrl());
            }
            if (StringUtil.isNotEmpty(systemProjectInfo.getProjectAbsolutePath())) {
                criteria.andProjectAbsolutePathEqualTo(systemProjectInfo.getProjectAbsolutePath());
            }
            if (null != systemProjectInfo.getTemplateId()) {
                criteria.andTemplateIdEqualTo(systemProjectInfo.getTemplateId());
            }
            if (StringUtil.isNotEmpty(systemProjectInfo.getProjectComment())) {
                criteria.andProjectCommentEqualTo(systemProjectInfo.getProjectComment());
            }
            if (StringUtil.isNotEmpty(systemProjectInfo.getDatabaseType())) {
                criteria.andDatabaseTypeEqualTo(systemProjectInfo.getDatabaseType());
            }
            if (StringUtil.isNotEmpty(systemProjectInfo.getJdbcDriverClass())) {
                criteria.andJdbcDriverClassEqualTo(systemProjectInfo.getJdbcDriverClass());
            }
            if (StringUtil.isNotEmpty(systemProjectInfo.getJdbcUserName())) {
                criteria.andJdbcUserNameEqualTo(systemProjectInfo.getJdbcUserName());
            }
            if (StringUtil.isNotEmpty(systemProjectInfo.getJdbcUserPassword())) {
                criteria.andJdbcUserPasswordEqualTo(systemProjectInfo.getJdbcUserPassword());
            }
            if (StringUtil.isNotEmpty(systemProjectInfo.getJdbcUrl())) {
                criteria.andJdbcUrlEqualTo(systemProjectInfo.getJdbcUrl());
            }
            if (null != systemProjectInfo.getCreateBy()) {
                criteria.andCreateByEqualTo(systemProjectInfo.getCreateBy());
            }
            if (null != systemProjectInfo.getCreateDateBegin()) {
                criteria.andCreateDateGreaterThanOrEqualTo(systemProjectInfo.getCreateDateBegin());
            }
            if (null != systemProjectInfo.getCreateDateEnd()) {
                criteria.andCreateDateLessThanOrEqualTo(systemProjectInfo.getCreateDateEnd());
            }
            if (null != systemProjectInfo.getUpdateBy()) {
                criteria.andUpdateByEqualTo(systemProjectInfo.getUpdateBy());
            }
            if (null != systemProjectInfo.getUpdateDateBegin()) {
                criteria.andUpdateDateGreaterThanOrEqualTo(systemProjectInfo.getUpdateDateBegin());
            }
            if (null != systemProjectInfo.getUpdateDateEnd()) {
                criteria.andUpdateDateLessThanOrEqualTo(systemProjectInfo.getUpdateDateEnd());
            }
        }
        return example;
    }
}
